/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.netbeans.modules.lexer.gen.javacc;

import java.lang.reflect.Field;
import java.lang.SecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.List;
import org.netbeans.modules.lexer.gen.MutableTokenId;
import org.netbeans.modules.lexer.gen.TokenTypes;

/**
 * Encapsulation of xxxConstants class generated by JavaCC.
 *
 * @author Miloslav Metelka
 * @version 1.00
 */

public class JavaCCTokenTypes extends TokenTypes {
    
    private boolean defaultStateFieldFound;
    
    private final Map name2sample = new HashMap();
    
    /**
     * Maximum value of state determined as maximum constant that occurs
     * in the xxxConstants class after DEFAULT field.
     */
    private int maxState;
    
    public JavaCCTokenTypes(Class tokenTypesClass) {
        super(tokenTypesClass);
    }
    
    protected void updateId(MutableTokenId id) {
        super.updateId(id);
        
        String sampleText = getSampleText(id.getTokenTypeName());
        if (sampleText != null) {
            id.addSampleText(sampleText);
        }
    }

    public String getSampleText(String tokenTypeName) {
        inspect();

        return (String)name2sample.get(tokenTypeName);
    }
    
    public int getMaxState() {
        return maxState;
    }

    protected boolean inspect() {
        if (!super.inspect()) {
            return false;
        }

        try {
            Field tokenImageField = getTokenTypesClass().getDeclaredField("tokenImage");
            String[] tokenImage = (String[])tokenImageField.get(null);

            for (int i = 0; i < tokenImage.length; i++) {
                String tokenTypeName = getTokenTypeName(i);
                if (tokenTypeName != null) {
                    String img = tokenImage[i];
                    if (img != null && img.startsWith("\"")) {
                        img = img.substring(1, img.length() - 1);
                        name2sample.put(tokenTypeName, img);
                    }
                }
            }

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        
        return true; // inspection really done
    }

    protected boolean isAccepted(String tokenTypeName, int tokenTypeValue) {
        if (!defaultStateFieldFound) {
            if ("DEFAULT".equals(tokenTypeName)) {
                defaultStateFieldFound = true;
            }

        } else { // default state already found
            maxState = Math.max(maxState, tokenTypeValue);
        }

        return !defaultStateFieldFound;
    }

}

